<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>统计分析</title>
	
    <link href="/include/css/smoothness/jquery-ui-1.10.3.custom.min.css" rel="stylesheet">
	<style type="text/css">
	.mytable {
	    width: 100%;
	    padding: 0;
	    margin: 0 auto;
	    text-algin: center;
	}
	caption {
	    padding: 0 0 5px 0;
	    font: italic 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
	    text-align: center;
	}
	th {
	    font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
	    color: #4f6b72;
	    border-right: 1px solid #C1DAD7;
	    border-bottom: 1px solid #C1DAD7;
	    border-top: 1px solid #C1DAD7;
	    letter-spacing: 2px;
	    text-transform: uppercase;
	    text-align: left;
	    padding: 6px 6px 6px 12px;
	    background-color: #CAE8EA;
	}
	td {
	    border-right: 1px solid #C1DAD7;
	    border-bottom: 1px solid #C1DAD7;
	    background: #fff;
	    font-size:11px;
	    padding: 6px 6px 6px 12px;
	    color: #4f6b72;
	}
	td.alt {
	    background: #F5FAFA;
	    color: #797268;
	}
	/*---------for IE 5.x bug*/
	html>body td{ font-size:11px;}
	</style>
	<script type="text/javascript" src="/include/js/jquery-1.9.1.js"></script>
	<script type="text/javascript" src="/include/js/jquery-ui-1.10.3.custom.min.js"></script>
	<script type="text/javascript" src="/include/js/jquery.ui.datepicker-zh-cn.js"></script>
	<script type="text/javascript">
	$(function(){
	    $("#time_start").datepicker({
	        buttonImage: "images/calendar.gif",
	        buttonImageOnly: true,
	        changeMonth: true,
	        changeYear: true
	    });
	    $("#time_end").datepicker({
	        buttonImage: "images/calendar.gif",
	        buttonImageOnly: true,
	        changeMonth: true,
	        changeYear: true
	    });
	    
	});
	</script>
</head>

<body>
<?php
require dirname(__FILE__) . '/checkuser.php';
require dirname(dirname(__FILE__)) . '/include/conf.php';
require dirname(dirname(__FILE__)) . '/include/database.php';

$nowtime = time();
$time_start = isset($_GET['time_start']) ? $_GET['time_start'] : date('Y-m-d', $nowtime);
$time_end = isset($_GET['time_end']) ? $_GET['time_end'] : date('Y-m-d', $nowtime);
$match_start = isset($_GET['match_start']) ? (int) $_GET['match_start'] : 0;
$match_end = isset($_GET['match_end']) ? (int) $_GET['match_end'] : 120;
$profit_rate = isset($_GET['profit_rate']) ? $_GET['profit_rate'] : 0;
$exclude_repet_pankou = (isset($_GET['exclude_repet_pankou']) && $_GET['exclude_repet_pankou'] == 'on') ? true : false;
$exclude_repet_match = (isset($_GET['exclude_repet_match']) && $_GET['exclude_repet_match'] == 'on') ? true : false;
$shuiwei_365 = isset($_GET['shuiwei_365']) ? $_GET['shuiwei_365'] : '';
$bet_big = (isset($_GET['bet_big']) && ($_GET['bet_big'] == 1 || $_GET['bet_big'] == 2)) ? $_GET['bet_big'] : 0;
$wanfa_type = isset($_GET['wtype']) ? (int) $_GET['wtype'] : 1;

// 检查参数合法性
if (strlen($time_start) != 10 || strlen($time_end) != 10){
	exit('parameter [time] error!');
}
if (!is_numeric($profit_rate)){
	exit('parameter [profit_rate] error!');
}
if (!empty($shuiwei_365) && !is_numeric($shuiwei_365)){
	exit('parameter [shuiwei_365] error!');
}

$where = "where fetch_time>='{$time_start} 00:00:00' and fetch_time<='{$time_end} 23:59:59' and match_result>-1 and profit_rate>={$profit_rate} and wanfa={$wanfa_type}"; // 
$sql = 'select id, match_time, match_name, home_team, visiting_team, home_team_id, visiting_team_id, pankou, over_365, under_365, over_188, under_188, bet_big, match_result, fetch_time, profit_rate ';
$sql .= "from match_paired {$where} order by home_team_id, fetch_time, pankou";

$db = db_touzhu();
$re = $db->query($sql);

$statistics = $liansai_arr = $stat_detail = array();
if (!empty($re)){
	foreach ($re as $v){
		$_key = md5($v['match_name']);
		$liansai_arr[$_key][] = $v;
	}
	foreach ($liansai_arr as $key => $liansai){
		$bet_times = $win_365_big = $win_365_small = $win_188_big = $win_188_small = $zoupan = 0;
		$pankou_arr = $match_same_arr = $match_arr = array();
		foreach ($liansai as $match){
			// 限制比赛时间
			$_time = explode(':', $match['match_time']);
			if (is_numeric($_time[0])){
				if ($_time[0] < $match_start || $_time[0] > $match_end){
					continue;
				}
			}else {
				continue;
			}
			
			// 判断365水位
			// 1. 365大跟188小配的，365小水位小于等于设置值的
			// 2. 365小跟188大配的，365大水位小于等于设置值的
			if ($bet_big > 0){
				if ($match['bet_big'] == $bet_big){
					if (is_numeric($shuiwei_365)){
						if ($bet_big == 1 && $match['under_365'] > $shuiwei_365){
							continue;
						}
						if ($bet_big == 2 && $match['over_365'] > $shuiwei_365){
							continue;
						}
					}
				}else {
					continue;
				}
			}
			
			// 去掉重复盘口
			if ($exclude_repet_pankou){
				$_key = $match['home_team_id'] . '_' . $match['visiting_team_id'] . '_' . $match['pankou'];
				if (!isset($pankou_arr[$_key])){
					$pankou_arr[$_key] = 0;
				}else {
					continue;
				}
			}
			
			// 去掉重复场次（留最早一场）
			if ($exclude_repet_match){
				$_key = $match['home_team_id'] . '_' . $match['visiting_team_id'] . '_' . $match['match_name'] . '_' . substr($match['fetch_time'], 0, 10);
				if (!isset($match_same_arr[$_key])){
					$match_same_arr[$_key] = 0;
				}else {
					continue;
				}
			}
			
			if ($match['match_result'] == $match['pankou']){
				$zoupan ++;
			}elseif ($match['match_result'] > $match['pankou']){ // 买大赢
				// bet_big  1.365大188小 2.188大365小
				if ($match['bet_big'] == 1){
					$win_365_big ++;
				}elseif ($match['bet_big'] == 2){
					$win_188_big ++;
				}
			}else { // 买小赢
				if ($match['bet_big'] == 1){
					$win_188_small ++;
				}elseif ($match['bet_big'] == 2){
					$win_365_small ++;
				}
			}
			$fetch_date = substr($match['fetch_time'], 0, 10);
			$_key = md5($match['home_team_id'] . '_' . $match['visiting_team_id'] . '_' . $fetch_date);
			if (!isset($match_arr[$_key])){
				$match_arr[$_key] = 0;
			}
			$bet_times ++;
			$stat_detail[$key][] = $match;
		}
		if ($bet_times == 0){
			continue;
		}
		$liansai_name = $liansai[0]['match_name'];
		
		$statistics[$key] = array(
			'match_num' => count($match_arr),
			'match_name' => $liansai_name,
			'bet_times' => $bet_times,
			'win_365_big' => $win_365_big,
			'win_365_small' => $win_365_small,
			'win_188_big' => $win_188_big,
			'win_188_small' => $win_188_small,
			'zoupan' => $zoupan
		);
	}
}
?>
<div style="margin: 20px;">
	<strong><?php echo $GLOBAL_WANFA_NUM[$wanfa_type]?></strong>
	<form action="" method="get">
	<table>
		<tr>
			<td align="left">
				抓取时间：<input type="text" id="time_start" name="time_start" value="<?php echo $time_start?>" size="8" /> - <input type="text" id="time_end" name="time_end" value="<?php echo $time_end?>" size="8" />
				比赛时间：<input type="text" name="match_start" value="<?php echo $match_start?>" size="1" /> - <input type="text" name="match_end" value="<?php echo $match_end?>" size="1" />(单位:分钟)
			</td>
			<td rowspan="2">
				<input type="hidden" name="wtype" value="<?php echo $wanfa_type?>">
				<input type="submit" value=" 查 询 " style="width: 80px;height: 30px;font-size: 16px">
			</td>
		</tr>
		<tr>
			<td align="left">
				获利率大于千分之：<input type="input" name="profit_rate" value="<?php echo $profit_rate?>" size="1">
				<select name="bet_big"><option value="0" <?php if ($bet_big == 0) echo 'selected="selected"'?>>选择配水方式</option><option value="1" <?php if ($bet_big == 1) echo 'selected="selected"'?>>365大配188小</option><option value="2" <?php if ($bet_big == 2) echo 'selected="selected"'?>>365小配188大</option></select>
				：<input type="input" name="shuiwei_365" value="<?php echo $shuiwei_365?>" size="1">
				<label><input type="checkbox" name="exclude_repet_pankou" <?php echo $exclude_repet_pankou ? 'checked="checked"' : ''?> />去掉重复盘口</label>
				<label><input type="checkbox" name="exclude_repet_match" <?php echo $exclude_repet_match ? 'checked="checked"' : ''?> />去掉重复场次</label>
			</td>
		</tr>
	</table>
	</form>
</div>


<table class="mytable" cellspacing="0">
  <tr>
	<th>联赛名称</th>
	<th>总投注次数</th>
	<th>场次数</th>
	<th>365赢</th>
	<th>188赢</th>
	<th>走盘</th>
	<th>365大赢</th>
	<th>365小赢</th>
	<th>188大赢</th>
	<th>188小赢</th>
	<th>详情</th>
  </tr>
<?php if (!empty($statistics)){
	$counter = 1;
	foreach ($statistics as $key => $per){
?>	
  <tr>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['match_name']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['bet_times']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['match_num']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['win_365_big'] + $per['win_365_small']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['win_188_big'] + $per['win_188_small']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['zoupan']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['win_365_big']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['win_365_small']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['win_188_big']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $per['win_188_small']?></td>
	<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><a href="javascript: void(0)" class="show_detail" sign="<?php echo $key?>">详情</a></td>
  </tr>
<?php $counter ++;}}?>
</table>


<!--br /><br />
<table class="mytable" cellspacing="0">
<caption>参照数据</caption>
  <tr>
	<th>比赛时间</th>
	<th>联赛名称</th>
	<th>主队id</th>
	<th>客队id</th>
	<th>盘口</th>
	<th>365大水</th>
	<th>365小水</th>
	<th>188大水</th>
	<th>188小水</th>
	<th>--</th>
	<th>结果</th>
  </tr>
<?php /*if (!empty($re)){
	foreach ($re as $per){
?>	
  <tr>
	<td><?php echo $per['match_time']?></td>
	<td><?php echo $per['match_name']?></td>
	<td><?php echo $per['home_team_id']?></td>
	<td><?php echo $per['visiting_team_id']?></td>
	<td><?php echo $per['pankou']?></td>
	<td><?php echo $per['over_365']?></td>
	<td><?php echo $per['under_365']?></td>
	<td><?php echo $per['over_188']?></td>
	<td><?php echo $per['under_188']?></td>
	<td><?php echo $per['bet_big']?></td>
	<td><?php echo $per['match_result']?></td>
  </tr>
<?php }}*/?>
</table-->

<!-- 弹出层 begin -->
<style>
.bg{display:none;position:fixed;width:100%;height:100%;background:#000;z-index:2;top:0;left:0;opacity:0.7;}
.content_popup{display:none;width:80%;height:80%;position:fixed;margin:0 auto;top:10%;left:10%;background:#fff;z-index:3;overflow:scroll;}
</style>
<div class="bg"></div>
<?php if (!empty($stat_detail)){
foreach ($stat_detail as $key => $per){
?>
<div class="content_popup" id="sign_<?php echo $key?>">
	<table class="mytable" cellspacing="0">
	<caption>详细</caption>
	  <tr>
		<th>比赛时间</th>
		<th>联赛名称</th>
		<th>主队id</th>
		<th>客队id</th>
		<th>盘口</th>
		<th>365大水</th>
		<th>365小水</th>
		<th>188大水</th>
		<th>188小水</th>
		<th>配水方式</th>
		<th>结果</th>
		<th>获利率</th>
	  </tr>
	  <?php $counter = 1; foreach ($per as $v){?>
	  <tr>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['match_time']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['match_name']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['home_team']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['visiting_team']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['pankou']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['over_365']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['under_365']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['over_188']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['under_188']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $GLOBAL_PEISHUIFANGSHI[$v['bet_big']]?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['match_result']?></td>
		<td<?php echo ($counter % 2 == 0) ? ' class="alt"' : ''?>><?php echo $v['profit_rate']?></td>
	  </tr>
	  <?php $counter ++;}?>
	</table>
</div>
<?php }}?>
<script type="text/javascript">
var curr_popup = '';
$(function(){
    $('.show_detail').click(function(){
    	$('.bg').fadeIn(200);
    	curr_popup = $(this).attr('sign');
    	$('#sign_'+curr_popup).fadeIn(400);
    });
    $('.bg').click(function(){
    	$('.bg').fadeOut(800);
    	$('#sign_'+curr_popup).fadeOut(800);
    });
});
</script>
<!-- 弹出层 end -->

</body>
</html>